<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>39-scroll相关属性和缓动动画</title>
  
    
      <meta 
        property="og:title" 
        content="39-scroll相关属性和缓动动画">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/39-scroll%E7%9B%B8%E5%85%B3%E5%B1%9E%E6%80%A7%E5%92%8C%E7%BC%93%E5%8A%A8%E5%8A%A8%E7%94%BB/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2004-03-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="JS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#scroll-%E7%9B%B8%E5%85%B3%E5%B1%9E%E6%80%A7"><span class="toc-text">scroll 相关属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#window-onscroll-%E6%96%B9%E6%B3%95"><span class="toc-text">window.onscroll() 方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81ScrollWidth-%E5%92%8C-scrollHeight"><span class="toc-text">1、ScrollWidth 和 scrollHeight</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81scrollTop-%E5%92%8C-scrollLeft"><span class="toc-text">2、scrollTop 和 scrollLeft</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#scrollTop-%E7%9A%84%E5%85%BC%E5%AE%B9%E6%80%A7"><span class="toc-text">scrollTop 的兼容性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E5%B7%B2%E7%BB%8F-DTD-%E5%A3%B0%E6%98%8E"><span class="toc-text">判断是否已经 DTD 声明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%86-scrollTop-%E5%92%8C-scrollLeft-%E8%BF%9B%E8%A1%8C%E5%B0%81%E8%A3%85"><span class="toc-text">将 scrollTop 和 scrollLeft 进行封装</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96-html-%E6%96%87%E6%A1%A3%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">获取 html 文档的方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#scrollTop-%E4%B8%BE%E4%BE%8B%EF%BC%9A%E5%9B%BA%E5%AE%9A%E5%AF%BC%E8%88%AA%E6%A0%8F"><span class="toc-text">scrollTop 举例：固定导航栏</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%93%E5%8A%A8%E5%8A%A8%E7%94%BB"><span class="toc-text">缓动动画</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E4%B8%AA%E5%87%BD%E6%95%B0"><span class="toc-text">三个函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%93%E5%8A%A8%E5%8A%A8%E7%94%BB%E7%9A%84%E5%8E%9F%E7%90%86"><span class="toc-text">缓动动画的原理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%93%E6%85%A2%E5%8A%A8%E7%94%BB%E7%9A%84%E5%B0%81%E8%A3%85%EF%BC%88%E8%A7%A3%E5%86%B3%E5%9B%9B%E8%88%8D%E4%BA%94%E5%85%A5%E7%9A%84%E9%97%AE%E9%A2%98%EF%BC%89"><span class="toc-text">缓慢动画的封装（解决四舍五入的问题）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#window-scrollTo-%E6%96%B9%E6%B3%95%E4%B8%BE%E4%BE%8B%EF%BC%9A%E8%BF%94%E5%9B%9E%E5%88%B0%E9%A1%B6%E9%83%A8%E5%B0%8F%E7%81%AB%E7%AE%AD"><span class="toc-text">window.scrollTo()方法举例：返回到顶部小火箭</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      39-scroll相关属性和缓动动画
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2004-02-29T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2004-03-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/JS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          JS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>4k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/JS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            JS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="scroll-相关属性"><a href="#scroll-相关属性" class="headerlink" title="scroll 相关属性"></a>scroll 相关属性</h2><h3 id="window-onscroll-方法"><a href="#window-onscroll-方法" class="headerlink" title="window.onscroll() 方法"></a>window.onscroll() 方法</h3><p>当我们用鼠标滚轮，滚动网页的时候，会触发 window.onscroll() 方法。效果如下：（注意看控制台的打印结果）</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180202_2258.gif" class="lozad post-image"src="http://img.smyhvae.com/20180202_2258.gif"></p>
<p>如果你需要做滚动监听，可以使用这个方法。</p>
<p>我们来看看和 scroll 相关的有哪些属性。</p>
<h3 id="1、ScrollWidth-和-scrollHeight"><a href="#1、ScrollWidth-和-scrollHeight" class="headerlink" title="1、ScrollWidth 和 scrollHeight"></a>1、ScrollWidth 和 scrollHeight</h3><p><code>ScrollWidth</code> 和 <code>scrollHeight</code>：获取元素<strong>整个滚动区域</strong>的宽、高。包括 width 和 padding，不包括 border和margin。</p>
<p><strong>注意</strong>：</p>
<p><code>scrollHeight</code> 的特点是：如果内容超出了盒子，<code>scrollHeight</code>为内容的高（包括超出的内容）；如果不超出，<code>scrollHeight</code>为盒子本身的高度。<code>ScrollWidth</code>同理。</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">padding</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">3px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border</span>: <span class="number">8px</span> solid red;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span></span><br><span class="line">    静，能寒窗苦守；动，能点石成金。</span><br><span class="line">    静，能寒窗苦守；动，能点石成金。</span><br><span class="line">    静，能寒窗苦守；动，能点石成金。</span><br><span class="line">    静，能寒窗苦守；动，能点石成金。</span><br><span class="line">    静，能寒窗苦守；动，能点石成金。</span><br><span class="line">    静，能寒窗苦守；动，能点石成金。</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> div = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;div&quot;</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// `scrollHeight` 的特点是：如果内容超出了盒子，`scrollHeight`为内容的高（包括超出的内容）；如果不超出，`scrollHeight`为盒子本身的高度。</span></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//IE8以下（不包括IE8），为盒子本身内容的高度。</span></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(div.<span class="property">scrollWidth</span>);</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(div.<span class="property">scrollHeight</span>);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>打印结果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180203_1235.png" class="lozad post-image"src="http://img.smyhvae.com/20180203_1235.png"></p>
<h3 id="2、scrollTop-和-scrollLeft"><a href="#2、scrollTop-和-scrollLeft" class="headerlink" title="2、scrollTop 和 scrollLeft"></a>2、scrollTop 和 scrollLeft</h3><ul>
<li><p><code>scrollLeft</code>：获取水平滚动条滚动的距离。</p>
</li>
<li><p><code>scrollTop</code>：获取垂直滚动条滚动的距离。</p>
</li>
</ul>
<p><strong>实战经验</strong>：</p>
<p>当某个元素满足<code>scrollHeight - scrollTop == clientHeight</code>时，说明垂直滚动条滚动到底了。</p>
<p>当某个元素满足<code>scrollWidth - scrollLeft == clientWidth</code>时，说明水平滚动条滚动到底了。</p>
<p>这个实战经验非常有用，可以用来判断用户是否已经将内容滑动到底了。比如说，有些场景下，希望用户能够看完“长长的活动规则”，才允许触发接下来的表单操作。</p>
<h3 id="scrollTop-的兼容性"><a href="#scrollTop-的兼容性" class="headerlink" title="scrollTop 的兼容性"></a>scrollTop 的兼容性</h3><p>如果要获取页面滚动的距离，scrollTop 这个属性的写法要注意兼容性，如下。</p>
<p>（1）如果文档没有 DTD 声明，写法为：</p>
<pre class="highlight"><span class="line"><span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollTop</span></span><br></pre>

<p>在没有 DTD 声明的情况下，要求是这种写法，chrome浏览器才能认出来。</p>
<p>（2）如果文档有 DTD 声明，写法为：</p>
<pre class="highlight"><span class="line"><span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollTop</span></span><br></pre>

<p>在有 DTD 声明的情况下，要求是这种写法，IE6、7、8才能认出来。</p>
<p>综合上面这两个，就诞生了一种兼容性的写法：</p>
<pre class="highlight"><span class="line"><span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollTop</span> || <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollTop</span> <span class="comment">//方式一</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollTop</span> + <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollTop</span>  <span class="comment">//方式二</span></span><br></pre>

<p>另外还有一种兼容性的写法：<code>window.pageYOffset</code> 和 <code>window.pageXOffset</code>。这种写法无视DTD的声明。这种写法支持的浏览器版本是：火狐&#x2F;谷歌&#x2F;ie9+。</p>
<p>综合上面的几种写法，为了兼容，不管有没有DTD，<strong>最终版的兼容性写法：</strong></p>
<pre class="highlight"><span class="line"><span class="variable language_">window</span>.<span class="property">pageYOffset</span> || <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollTop</span> || <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollTop</span>;</span><br></pre>

<h3 id="判断是否已经-DTD-声明"><a href="#判断是否已经-DTD-声明" class="headerlink" title="判断是否已经 DTD 声明"></a>判断是否已经 DTD 声明</h3><p>方法如下：</p>
<pre class="highlight"><span class="line"><span class="variable language_">document</span>.<span class="property">compatMode</span> === <span class="string">&quot;CSS1Compat&quot;</span>   <span class="comment">// 已声明</span></span><br><span class="line"><span class="variable language_">document</span>.<span class="property">compatMode</span> === <span class="string">&quot;BackCompat&quot;</span>   <span class="comment">// 未声明</span></span><br></pre>

<h3 id="将-scrollTop-和-scrollLeft-进行封装"><a href="#将-scrollTop-和-scrollLeft-进行封装" class="headerlink" title="将 scrollTop 和 scrollLeft 进行封装"></a>将 scrollTop 和 scrollLeft 进行封装</h3><p>这里，我们将 scrollTop 和 scrollLeft 封装为一个方法，名叫scroll()，返回值为 一个对象。以后就直接调用<code>scroll().top</code> 和 <code>scroll().left</code>就好。</p>
<p>代码实现：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">body</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">6000px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">5000px</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//需求：封装一个兼容的scroll().返回的是对象，用scroll().top获取scrollTop，用scroll().left获取scrollLeft</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">window</span>.<span class="property">onscroll</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript"><span class="comment">//        var myScroll = scroll();</span></span></span><br><span class="line"><span class="language-javascript"><span class="comment">//        myScroll.top;</span></span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">scroll</span>().<span class="property">top</span>);</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">scroll</span>().<span class="property">left</span>);</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//函数封装（简单封装，实际工作使用）</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">function</span> <span class="title function_">scroll</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">return</span> &#123; <span class="comment">//此函数的返回值是对象</span></span></span><br><span class="line"><span class="language-javascript">            <span class="attr">left</span>: <span class="variable language_">window</span>.<span class="property">pageYOffset</span> || <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollTop</span> || <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollTop</span>,</span></span><br><span class="line"><span class="language-javascript">            <span class="attr">right</span>: <span class="variable language_">window</span>.<span class="property">pageXOffset</span> || <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollLeft</span> || <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollLeft</span></span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>上方代码中，函数定义的那部分就是要封装的代码。</p>
<p>另外还有一种比较麻烦的封装方式：（仅供参考）</p>
<pre class="highlight"><span class="line"><span class="keyword">function</span> <span class="title function_">scroll</span>(<span class="params"></span>) &#123;  <span class="comment">// 开始封装自己的scrollTop</span></span><br><span class="line">    <span class="keyword">if</span>(<span class="variable language_">window</span>.<span class="property">pageYOffset</span> !== <span class="literal">undefined</span>) &#123;  <span class="comment">// ie9+ 高版本浏览器</span></span><br><span class="line">        <span class="comment">// 因为 window.pageYOffset 默认的是  0  所以这里需要判断</span></span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="attr">left</span>: <span class="variable language_">window</span>.<span class="property">pageXOffset</span>,</span><br><span class="line">            <span class="attr">top</span>: <span class="variable language_">window</span>.<span class="property">pageYOffset</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span>(<span class="variable language_">document</span>.<span class="property">compatMode</span> === <span class="string">&quot;CSS1Compat&quot;</span>) &#123;    <span class="comment">// 标准浏览器   来判断有没有声明DTD</span></span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="attr">left</span>: <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollLeft</span>,</span><br><span class="line">            <span class="attr">top</span>: <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollTop</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123;   <span class="comment">// 未声明 DTD</span></span><br><span class="line">        <span class="attr">left</span>: <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollLeft</span>,</span><br><span class="line">        <span class="attr">top</span>: <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollTop</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre>

<h2 id="获取-html-文档的方法"><a href="#获取-html-文档的方法" class="headerlink" title="获取 html 文档的方法"></a>获取 html 文档的方法</h2><p>获取title、body、head、html标签的方法如下：</p>
<ul>
<li><p><code>document.title</code> 文档标题；</p>
</li>
<li><p><code>document.head</code>  文档的头标签</p>
</li>
<li><p><code>document.body</code>  文档的body标签；</p>
</li>
<li><p><code>document.documentElement</code>  （这个很重要）。</p>
</li>
</ul>
<p><code>document.documentElement</code>表示文档的html标签。也就是说，基本结构当中的 <code>html 标签</code>而是通过<code>document.documentElement</code>访问的，并不是通过 document.html 去访问的。</p>
<h2 id="scrollTop-举例：固定导航栏"><a href="#scrollTop-举例：固定导航栏" class="headerlink" title="scrollTop 举例：固定导航栏"></a>scrollTop 举例：固定导航栏</h2><p>完整版代码实现：</p>
<p>（1）index.html：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        * &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">padding</span>: <span class="number">0</span></span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">img</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">vertical-align</span>: top;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.main</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">0</span> auto;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">1000px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin-top</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-id">#Q-nav1</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">overflow</span>: hidden;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.fixed</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: fixed;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">top</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">left</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--引入工具js--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;tools.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">window</span>.<span class="property">onload</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//需求1：当我们滚动界面的时候，被卷曲的头部如果超过第二个盒子距离顶部的位置，那么直接给第二个盒子加类名.fixed</span></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//需求2：当我们滚动界面的时候，被卷曲的头部如果小于第二个盒子距离顶部的位置，那么直接给第二个盒子取消类名.fixed</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//1.老三步。</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> topDiv = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;top&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> height = topDiv.<span class="property">offsetHeight</span>;</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> middle = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;Q-nav1&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> main = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;main&quot;</span>);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">window</span>.<span class="property">onscroll</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//2.判断 ，被卷曲的头部的大小</span></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">if</span> (<span class="title function_">scroll</span>().<span class="property">top</span> &gt; height) &#123;</span></span><br><span class="line"><span class="language-javascript">                    <span class="comment">//3.满足条件添加类，否则删除类</span></span></span><br><span class="line"><span class="language-javascript">                    middle.<span class="property">className</span> += <span class="string">&quot; fixed&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">                    <span class="comment">//第二个盒子也要占位置，为了避免重叠，我们给第三个盒子一个上padding的空间，把这个空间留给第二个盒子</span></span></span><br><span class="line"><span class="language-javascript">                    main.<span class="property">style</span>.<span class="property">paddingTop</span> = middle.<span class="property">offsetHeight</span> + <span class="string">&quot;px&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">                &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="language-javascript">                    middle.<span class="property">className</span> = <span class="string">&quot;&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">                    <span class="comment">//清零</span></span></span><br><span class="line"><span class="language-javascript">                    main.<span class="property">style</span>.<span class="property">paddingTop</span> = <span class="number">0</span>;</span></span><br><span class="line"><span class="language-javascript">                &#125;</span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;top&quot;</span> <span class="attr">id</span>=<span class="string">&quot;top&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;images/top.png&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;Q-nav1&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;images/nav.png&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;main&quot;</span> <span class="attr">id</span>=<span class="string">&quot;main&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;images/main.png&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br></pre>

<p>上方代码中，有一个技巧：</p>
<pre class="highlight"><span class="line">main.<span class="property">style</span>.<span class="property">paddingTop</span> = middle.<span class="property">offsetHeight</span> + <span class="string">&quot;px&quot;</span>;</span><br></pre>

<p>仔细看注释就好。</p>
<p>（2）tools.js：</p>
<pre class="highlight"><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by smyhvae on 2018/02/03.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">scroll</span>(<span class="params"></span>) &#123;  <span class="comment">// 开始封装自己的scrollTop</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">window</span>.<span class="property">pageYOffset</span> !== <span class="literal">undefined</span>) &#123;  <span class="comment">// ie9+ 高版本浏览器</span></span><br><span class="line">        <span class="comment">// 因为 window.pageYOffset 默认的是  0  所以这里需要判断</span></span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="attr">left</span>: <span class="variable language_">window</span>.<span class="property">pageXOffset</span>,</span><br><span class="line">            <span class="attr">top</span>: <span class="variable language_">window</span>.<span class="property">pageYOffset</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">compatMode</span> === <span class="string">&quot;CSS1Compat&quot;</span>) &#123;    <span class="comment">// 标准浏览器   来判断有没有声明DTD</span></span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="attr">left</span>: <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollLeft</span>,</span><br><span class="line">            <span class="attr">top</span>: <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollTop</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123;   <span class="comment">// 未声明 DTD</span></span><br><span class="line">        <span class="attr">left</span>: <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollLeft</span>,</span><br><span class="line">        <span class="attr">top</span>: <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollTop</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre>


<p>实现效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180203_1619.gif" class="lozad post-image"src="http://img.smyhvae.com/20180203_1619.gif"></p>
<p><del>工程文件</del>：</p>
<ul>
<li><p>2018-02-03-scrollTop固定导航栏.rar</p>
</li>
<li><p>下载链接暂无。</p>
</li>
</ul>
<h2 id="缓动动画"><a href="#缓动动画" class="headerlink" title="缓动动画"></a>缓动动画</h2><h3 id="三个函数"><a href="#三个函数" class="headerlink" title="三个函数"></a>三个函数</h3><p>缓慢动画里，我们要用到三个函数，这里先列出来：</p>
<ul>
<li><p>Math.ceil()         向上取整</p>
</li>
<li><p>Math.floor()        向下取整</p>
</li>
<li><p>Math.round();   四舍五入</p>
</li>
</ul>
<h3 id="缓动动画的原理"><a href="#缓动动画的原理" class="headerlink" title="缓动动画的原理"></a>缓动动画的原理</h3><p>缓动动画的原理就是：在移动的过程中，步长越来越小。</p>
<p>设置步长为：<strong>目标位置和盒子当前位置的十分之一</strong>。用公式表达，即：</p>
<pre class="highlight"><span class="line">盒子位置 = 盒子本身位置 + (目标位置 - 盒子本身位置)/ 10；</span><br></pre>

<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: pink;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span>&gt;</span>运动到left = 400px<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> btn = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;button&quot;</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> div = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;div&quot;</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    btn.<span class="property">onclick</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="built_in">setInterval</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//动画原理：盒子未来的位置 = 盒子当前的位置+步长</span></span></span><br><span class="line"><span class="language-javascript">            div.<span class="property">style</span>.<span class="property">left</span> = div.<span class="property">offsetLeft</span> + (<span class="number">400</span> - div.<span class="property">offsetLeft</span>) / <span class="number">10</span> + <span class="string">&quot;px&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">        &#125;, <span class="number">30</span>);</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180202_2046.gif" class="lozad post-image"src="http://img.smyhvae.com/20180202_2046.gif"></p>
<h3 id="缓慢动画的封装（解决四舍五入的问题）"><a href="#缓慢动画的封装（解决四舍五入的问题）" class="headerlink" title="缓慢动画的封装（解决四舍五入的问题）"></a>缓慢动画的封装（解决四舍五入的问题）</h3><p>我们发现一个问题，上图中的盒子最终并没有到达400px的位置，而是只到了396.04px就停住了：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180202_2140.png" class="lozad post-image"src="http://img.smyhvae.com/20180202_2140.png"></p>
<p>原因是：JS在取整的运算时，进行了四舍五入。</p>
<p>我们把打印396.04px这个left值打印出来看看：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180202_2150.png" class="lozad post-image"src="http://img.smyhvae.com/20180202_2150.png"></p>
<p>我么发现，通过<code>div.style.left</code>获取的值是精确的，通过<code>div.offsetLeft</code>获取的left值会进行四舍五入。</p>
<p>此时，我们就要用到取整的函数了。</p>
<p>通过对缓动动画进行封装，完整版的代码实现如下：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">background-color</span>: pink;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: absolute;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">left</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span>&gt;</span>运动到200<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span>&gt;</span>运动到400<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> btn = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;button&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> div = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;div&quot;</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    btn[<span class="number">0</span>].<span class="property">onclick</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="title function_">animate</span>(div, <span class="number">200</span>);</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    btn[<span class="number">1</span>].<span class="property">onclick</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="title function_">animate</span>(div, <span class="number">400</span>);</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//缓动动画封装</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">function</span> <span class="title function_">animate</span>(<span class="params">ele, target</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//要用定时器，先清定时器</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//一个萝卜一个坑儿，一个元素对应一个定时器</span></span></span><br><span class="line"><span class="language-javascript">        <span class="built_in">clearInterval</span>(ele.<span class="property">timer</span>);</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//定义定时器</span></span></span><br><span class="line"><span class="language-javascript">        ele.<span class="property">timer</span> = <span class="built_in">setInterval</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//获取步长</span></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//步长应该是越来越小的，缓动的算法。</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> step = (target - ele.<span class="property">offsetLeft</span>) / <span class="number">10</span>;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//对步长进行二次加工(大于0向上取整,小于0向下取整)</span></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//达到的效果是：最后10像素的时候都是1像素1像素的向目标位置移动，就能够到达指定位置。</span></span></span><br><span class="line"><span class="language-javascript">            step = step &gt; <span class="number">0</span> ? <span class="title class_">Math</span>.<span class="title function_">ceil</span>(step) : <span class="title class_">Math</span>.<span class="title function_">floor</span>(step);</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//动画原理： 目标位置 = 当前位置 + 步长</span></span></span><br><span class="line"><span class="language-javascript">            ele.<span class="property">style</span>.<span class="property">left</span> = ele.<span class="property">offsetLeft</span> + step + <span class="string">&quot;px&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">console</span>.<span class="title function_">log</span>(step);</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//检测缓动动画有没有停止</span></span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;smyhvae&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">if</span> (<span class="title class_">Math</span>.<span class="title function_">abs</span>(target - ele.<span class="property">offsetLeft</span>) &lt;= <span class="title class_">Math</span>.<span class="title function_">abs</span>(step)) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//处理小数赋值</span></span></span><br><span class="line"><span class="language-javascript">                ele.<span class="property">style</span>.<span class="property">left</span> = target + <span class="string">&quot;px&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">                <span class="built_in">clearInterval</span>(ele.<span class="property">timer</span>);</span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript">        &#125;, <span class="number">30</span>);</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>实现效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180202_2239.gif" class="lozad post-image"src="http://img.smyhvae.com/20180202_2239.gif"></p>
<h2 id="window-scrollTo-方法举例：返回到顶部小火箭"><a href="#window-scrollTo-方法举例：返回到顶部小火箭" class="headerlink" title="window.scrollTo()方法举例：返回到顶部小火箭"></a>window.scrollTo()方法举例：返回到顶部小火箭</h2><p>（1）index.html：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">img</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">position</span>: fixed;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">bottom</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">right</span>: <span class="number">50px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">cursor</span>: pointer;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">display</span>: none;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">div</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">1210px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">100px</span> auto;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">text-align</span>: center;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">font</span>: <span class="number">500</span> <span class="number">26px</span>/<span class="number">35px</span> <span class="string">&quot;simsun&quot;</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;tools.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">window</span>.<span class="property">onload</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//需求：被卷去的头部超过100显示小火箭，然后点击小火箭屏幕缓慢移动到最顶端。</span></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//难点：我们以前是移动盒子，现在是移动屏幕，我们没有学过如何移动屏幕。</span></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//      技术点：window.scrollTo(x,y);浏览器显示区域跳转到指定的坐标</span></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//步骤：</span></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//1.老三步</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> img = <span class="variable language_">document</span>.<span class="title function_">getElementsByTagName</span>(<span class="string">&quot;img&quot;</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">window</span>.<span class="property">onscroll</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//被卷去的距离大于200显示小火箭，否则隐藏</span></span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//2.显示隐藏小火箭</span></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">if</span> (<span class="title function_">scroll</span>().<span class="property">top</span> &gt; <span class="number">1000</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                    img.<span class="property">style</span>.<span class="property">display</span> = <span class="string">&quot;block&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">                &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="language-javascript">                    img.<span class="property">style</span>.<span class="property">display</span> = <span class="string">&quot;none&quot;</span>;</span></span><br><span class="line"><span class="language-javascript">                &#125;</span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//每次移动滚动条的时候都给leader赋值，模拟leader获取距离顶部的距离</span></span></span><br><span class="line"><span class="language-javascript">                leader = <span class="title function_">scroll</span>().<span class="property">top</span>;</span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//3.缓动跳转到页面最顶端（利用我们的缓动动画）</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> timer = <span class="literal">null</span>;</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> target = <span class="number">0</span>; <span class="comment">//目标位置</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> leader = <span class="number">0</span>; <span class="comment">//显示区域自身的位置</span></span></span><br><span class="line"><span class="language-javascript">            img.<span class="property">onclick</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//技术点：window.scrollTo(0,0);</span></span></span><br><span class="line"><span class="language-javascript">                <span class="comment">//要用定时器，先清定时器</span></span></span><br><span class="line"><span class="language-javascript">                <span class="built_in">clearInterval</span>(timer);</span></span><br><span class="line"><span class="language-javascript">                timer = <span class="built_in">setInterval</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                    <span class="comment">//获取步长</span></span></span><br><span class="line"><span class="language-javascript">                    <span class="keyword">var</span> step = (target - leader) / <span class="number">10</span>;</span></span><br><span class="line"><span class="language-javascript">                    <span class="comment">//二次处理步长</span></span></span><br><span class="line"><span class="language-javascript">                    step = step &gt; <span class="number">0</span> ? <span class="title class_">Math</span>.<span class="title function_">ceil</span>(step) : <span class="title class_">Math</span>.<span class="title function_">floor</span>(step);</span></span><br><span class="line"><span class="language-javascript">                    leader = leader + step; <span class="comment">//往上移动的过程中，step是负数。当前位置减去步长，就等于下一步的位置。</span></span></span><br><span class="line"><span class="language-javascript">                    <span class="comment">//显示区域移动</span></span></span><br><span class="line"><span class="language-javascript">                    <span class="variable language_">window</span>.<span class="title function_">scrollTo</span>(<span class="number">0</span>, leader);</span></span><br><span class="line"><span class="language-javascript">                    <span class="comment">//清除定时器</span></span></span><br><span class="line"><span class="language-javascript">                    <span class="keyword">if</span> (leader === <span class="number">0</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                        <span class="built_in">clearInterval</span>(timer);</span></span><br><span class="line"><span class="language-javascript">                    &#125;</span></span><br><span class="line"><span class="language-javascript">                &#125;, <span class="number">25</span>);</span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;images/Top.jpg&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    我是最顶端.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    生命壹号，永不止步.....<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>（2）tools.js:</p>
<pre class="highlight"><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by smyhvae on 2015/12/8.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//函数：获取scrollTop和scrollLeft的值</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">scroll</span>(<span class="params"></span>) &#123;  <span class="comment">// 开始封装自己的scrollTop</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">window</span>.<span class="property">pageYOffset</span> != <span class="literal">null</span>) &#123;  <span class="comment">// ie9+ 高版本浏览器</span></span><br><span class="line">        <span class="comment">// 因为 window.pageYOffset 默认的是  0  所以这里需要判断</span></span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="attr">left</span>: <span class="variable language_">window</span>.<span class="property">pageXOffset</span>,</span><br><span class="line">            <span class="attr">top</span>: <span class="variable language_">window</span>.<span class="property">pageYOffset</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (<span class="variable language_">document</span>.<span class="property">compatMode</span> === <span class="string">&quot;CSS1Compat&quot;</span>) &#123;    <span class="comment">// 标准浏览器   来判断有没有声明DTD</span></span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="attr">left</span>: <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollLeft</span>,</span><br><span class="line">            <span class="attr">top</span>: <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">scrollTop</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123;   <span class="comment">// 未声明 DTD</span></span><br><span class="line">        <span class="attr">left</span>: <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollLeft</span>,</span><br><span class="line">        <span class="attr">top</span>: <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollTop</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>

<p>实现效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180203_1710.gif" class="lozad post-image"src="http://img.smyhvae.com/20180203_1710.gif"></p>
<p>小火箭的图片资源：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180203-Top.jpg" class="lozad post-image"src="http://img.smyhvae.com/20180203-Top.jpg"></p>
<h2 id="我的公众号"><a href="#我的公众号" class="headerlink" title="我的公众号"></a>我的公众号</h2><p>想学习<strong>更多技能</strong>？不妨关注我的微信公众号：<strong>千古壹号</strong>（id：<code>qianguyihao</code>）。</p>
<p>扫一扫，你将发现另一个全新的世界，而这将是一场美丽的意外：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20190101.png" class="lozad post-image"src="http://img.smyhvae.com/20190101.png"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/39-scroll%E7%9B%B8%E5%85%B3%E5%B1%9E%E6%80%A7%E5%92%8C%E7%BC%93%E5%8A%A8%E5%8A%A8%E7%94%BB/">
            https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/39-scroll%E7%9B%B8%E5%85%B3%E5%B1%9E%E6%80%A7%E5%92%8C%E7%BC%93%E5%8A%A8%E5%8A%A8%E7%94%BB/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/41-%E4%BA%8B%E4%BB%B6%E7%9A%84%E7%BB%91%E5%AE%9A%E5%92%8C%E4%BA%8B%E4%BB%B6%E5%AF%B9%E8%B1%A1Event/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">41-事件的绑定和事件对象Event </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/42-%E4%BA%8B%E4%BB%B6%E7%9A%84%E4%BC%A0%E6%92%AD%E5%92%8C%E4%BA%8B%E4%BB%B6%E5%86%92%E6%B3%A1/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">42-事件的传播和事件冒泡 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#scroll-%E7%9B%B8%E5%85%B3%E5%B1%9E%E6%80%A7"><span class="toc-text">scroll 相关属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#window-onscroll-%E6%96%B9%E6%B3%95"><span class="toc-text">window.onscroll() 方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81ScrollWidth-%E5%92%8C-scrollHeight"><span class="toc-text">1、ScrollWidth 和 scrollHeight</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81scrollTop-%E5%92%8C-scrollLeft"><span class="toc-text">2、scrollTop 和 scrollLeft</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#scrollTop-%E7%9A%84%E5%85%BC%E5%AE%B9%E6%80%A7"><span class="toc-text">scrollTop 的兼容性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E5%B7%B2%E7%BB%8F-DTD-%E5%A3%B0%E6%98%8E"><span class="toc-text">判断是否已经 DTD 声明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%86-scrollTop-%E5%92%8C-scrollLeft-%E8%BF%9B%E8%A1%8C%E5%B0%81%E8%A3%85"><span class="toc-text">将 scrollTop 和 scrollLeft 进行封装</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96-html-%E6%96%87%E6%A1%A3%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">获取 html 文档的方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#scrollTop-%E4%B8%BE%E4%BE%8B%EF%BC%9A%E5%9B%BA%E5%AE%9A%E5%AF%BC%E8%88%AA%E6%A0%8F"><span class="toc-text">scrollTop 举例：固定导航栏</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%93%E5%8A%A8%E5%8A%A8%E7%94%BB"><span class="toc-text">缓动动画</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E4%B8%AA%E5%87%BD%E6%95%B0"><span class="toc-text">三个函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%93%E5%8A%A8%E5%8A%A8%E7%94%BB%E7%9A%84%E5%8E%9F%E7%90%86"><span class="toc-text">缓动动画的原理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%93%E6%85%A2%E5%8A%A8%E7%94%BB%E7%9A%84%E5%B0%81%E8%A3%85%EF%BC%88%E8%A7%A3%E5%86%B3%E5%9B%9B%E8%88%8D%E4%BA%94%E5%85%A5%E7%9A%84%E9%97%AE%E9%A2%98%EF%BC%89"><span class="toc-text">缓慢动画的封装（解决四舍五入的问题）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#window-scrollTo-%E6%96%B9%E6%B3%95%E4%B8%BE%E4%BE%8B%EF%BC%9A%E8%BF%94%E5%9B%9E%E5%88%B0%E9%A1%B6%E9%83%A8%E5%B0%8F%E7%81%AB%E7%AE%AD"><span class="toc-text">window.scrollTo()方法举例：返回到顶部小火箭</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#scroll-%E7%9B%B8%E5%85%B3%E5%B1%9E%E6%80%A7"><span class="toc-text">scroll 相关属性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#window-onscroll-%E6%96%B9%E6%B3%95"><span class="toc-text">window.onscroll() 方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81ScrollWidth-%E5%92%8C-scrollHeight"><span class="toc-text">1、ScrollWidth 和 scrollHeight</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81scrollTop-%E5%92%8C-scrollLeft"><span class="toc-text">2、scrollTop 和 scrollLeft</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#scrollTop-%E7%9A%84%E5%85%BC%E5%AE%B9%E6%80%A7"><span class="toc-text">scrollTop 的兼容性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E5%B7%B2%E7%BB%8F-DTD-%E5%A3%B0%E6%98%8E"><span class="toc-text">判断是否已经 DTD 声明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%86-scrollTop-%E5%92%8C-scrollLeft-%E8%BF%9B%E8%A1%8C%E5%B0%81%E8%A3%85"><span class="toc-text">将 scrollTop 和 scrollLeft 进行封装</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96-html-%E6%96%87%E6%A1%A3%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">获取 html 文档的方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#scrollTop-%E4%B8%BE%E4%BE%8B%EF%BC%9A%E5%9B%BA%E5%AE%9A%E5%AF%BC%E8%88%AA%E6%A0%8F"><span class="toc-text">scrollTop 举例：固定导航栏</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%93%E5%8A%A8%E5%8A%A8%E7%94%BB"><span class="toc-text">缓动动画</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E4%B8%AA%E5%87%BD%E6%95%B0"><span class="toc-text">三个函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%93%E5%8A%A8%E5%8A%A8%E7%94%BB%E7%9A%84%E5%8E%9F%E7%90%86"><span class="toc-text">缓动动画的原理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%93%E6%85%A2%E5%8A%A8%E7%94%BB%E7%9A%84%E5%B0%81%E8%A3%85%EF%BC%88%E8%A7%A3%E5%86%B3%E5%9B%9B%E8%88%8D%E4%BA%94%E5%85%A5%E7%9A%84%E9%97%AE%E9%A2%98%EF%BC%89"><span class="toc-text">缓慢动画的封装（解决四舍五入的问题）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#window-scrollTo-%E6%96%B9%E6%B3%95%E4%B8%BE%E4%BE%8B%EF%BC%9A%E8%BF%94%E5%9B%9E%E5%88%B0%E9%A1%B6%E9%83%A8%E5%B0%8F%E7%81%AB%E7%AE%AD"><span class="toc-text">window.scrollTo()方法举例：返回到顶部小火箭</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
